<html>
<head>
    <meta charset="utf-8">
    <base href="<%=basePath %>">
    <title>图书管理系统——图书管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="./layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="./layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">图书编号：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="no" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">书名：</label>
                    <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">图书分类：</label>
                    <div class="layui-input-inline" >
                        <select name="type">
                            <option value="">请选择</option>
                            <option value="文学类">文学类</option>
                            <option value="历史类">历史类</option>
                            <option value="编程类">编程类</option>
                            <option value="法律类">法律类</option>
                            <option value="小说类">小说类</option>
                            <option value="其他类">其他类</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-books-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn">搜索</i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card-body">
        <div style="padding-bottom: 10px; ">

            <button class="layui-btn layui-btn-normal layuiadmin-btn-list" data-type="add" lay-active="add">
                添加
            </button>
            <button class="layui-btn layui-btn-danger layuiadmin-btn-list" data-type="batchDel">
                删除
            </button>
        </div>


        <table id="LAY-app-books-list" lay-filter="LAY-app-books-list"></table>
        <script type="text/html" id="table-books-list">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
        </div>
    </div>
</div>

<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="./layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: './layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var table = layui.table
            , form = layui.form;

        //监听搜索
        form.on('submit(LAY-app-books-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-app-books-list', {
                where: field
            });
        });

        //加载表格数据
        table.render({
            elem: '#LAY-app-books-list',
            url: '/booksList',
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            page: true,
            limit: 10,
            limits: [10, 20, 30, 50],  //数据分页条
            text: '对不起，加载出现异常！',
            cols: [[
                {fixed: 'left',type:'checkbox'},
                // {field: 'id', title: '序号', width:120},
                {field: 'no', title: '图书编号', width:120},
                {field: 'name', title: '图书名称', width:280},
                {field: 'type', title: '图书类型', width:120},
                {field: 'author', title: '作者', width:120},
                {field: 'price', title: '价格', width:120},
                {field: 'language', title: '语言', width:120},
                {fixed: 'right', title: '操作', width:200, align: 'center', toolbar: '#table-books-list'}
            ]]
            //表格数据渲染
        })

        /*
          var $ = layui.$, active = {} 可用于执行表格数据的重载操作
            点击新增仓库 按钮时，添加表格，并重新加载表格数据
        */


        var $ = layui.$, active = {
            add: function () {
                console.log(123)
                layer.open({
                    type: 2,
                    title: '新增',
                    content: '/books_add.html', //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                    maxmin: true,
                    area: ['550px', '550px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        //当点击确定是，出发iframe的提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            },

            batchDel: function () {
                var checkStatus = table.checkStatus('LAY-app-books-list')
                var data = checkStatus.data;
                if (data.length == 0) {
                    return;
                }
                var arr = [];
                data.forEach(function (value, index) {
                    arr.push(value.id);
                });
                console.log(arr.join(","));
                $.ajax({
                    url: '/booksDelete',
                    data:{"id":arr.join(",")},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 0) {
                            var curr = parseInt($(".layui-laypage-curr em:last-child").text())  //获取当前页
                            if (table.cache['LAY-app-books-list'].length == 1 && curr > 1) {
                                curr -= 1;
                            }
                            //添加成功
                            layer.msg('删除成功', {
                                offset: '15px'
                                , icon: 1
                                , time: 100
                            }, function () {
                                table.reload("LAY-app-books-list", {page: curr});//重载表格
                            });
                        } else if (result.code == 1) {
                            layer.open({
                                title: '提示'
                                , content: result.msg
                            });
                        }
                    }
                })
            }
        };

        //给layuiadmin-btn-list中的按钮，绑定点击事件，点击按钮时，
        // 触发上面 var $ = layui.$, active 中的某个函数
        $('.layui-btn.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //监听工具条， 当用户点击，编辑，删除按钮时，触发此函数
        table.on('tool(LAY-app-books-list)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获取具体事件

            //事件和  lay-event="edit"中值相等，则触发修改操作
            if (layEvent === 'edit') { //
                layer.open({
                    type: 2,
                    title: '编辑',
                    content: '/books_edit.html',
                    maxmin: true,
                    area: ['550px', '550px'],
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        //编辑页的数据回填
                        console.log(obj.data)
                        //编辑页的数据回填
                        body.find("#id").val(obj.data.id);
                        body.find("#no").val(obj.data.no);
                        body.find("#name").val(obj.data.name);
                        body.find("#type").val(obj.data.type);
                        body.find("#author").val(obj.data.author);
                        body.find("#price").val(obj.data.price);
                        body.find("#language").val(obj.data.language);
                    },
                    yes: function (index, layero) {
                        //当点击确定是，出发iframe的提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }

            //如果事件和  lay-event="del"中值相等，则触发删除操作
            if (layEvent === 'del') { //
                var id = data.id;

                $.ajax({
                    url: '/booksDelete',
                    data:{"id":id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 0) {
                            var curr = parseInt($(".layui-laypage-curr em:last-child").text())  //获取当前页
                            if (table.cache['LAY-app-books-list'].length == 1 && curr > 1) {
                                curr -= 1;
                            }
                            //添加成功
                            layer.msg('删除成功', {
                                offset: '15px'
                                , icon: 1
                                , time: 100
                            }, function () {
                                table.reload("LAY-app-books-list", {page: curr});//重载表格
                            });
                        } else if (result.code == 1) {
                            layer.open({
                                title: '提示'
                                , content: result.msg
                            });
                        }
                    }
                })
            }
        });
    });

    function reset() {
        $("select#deptId").val("");
        layui.use('form', function () {
            var form = layui.form;
            form.render("select");
        });
    }
</script>
</body>
</html>
